//
//  ByDocumentaryCreateVC.m
//  caipiao_ios
//
//  Created by mc on 2018/8/17.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByDocumentaryCreateVC.h"
#import "ByDocumentaryCellTitleView.h"
#import "ByDocumentaryContentView.h"
#import "ByDocumentaryOddsView.h"
#import "ByDocumentarySetChooseView.h"
#import "ByTextView.h"




@interface ByDocumentaryCreateVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ByTextView *textView;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) ByDocumentaryOddsView *oddsView;
@property (nonatomic, strong) UILabel *countLabel;

@property (nonatomic, assign) NSInteger public_type;

@property (nonatomic, strong) ByPayBombView *payBombView;

@property (nonatomic, strong) ByNetModel *orderModel;

@end

@implementation ByDocumentaryCreateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    self.title = @"发起跟单";
    
    [self initViews];
    
    self.public_type = 2;
    // Do any additional setup after loading the view.
}

- (ByPayBombView *)payBombView{
    if (_payBombView == nil) {
        _payBombView = [[ByPayBombView alloc] init];
        __weak typeof(self) weakSelf = self;
        _payBombView.sureBlock = ^(){
            [weakSelf.payBombView remove];
            weakSelf.payBombView = nil;
            [weakSelf leftItemsAction:0];
        };
        
        _payBombView.otherSureBlock = ^(NSString *str,NSInteger type){
            
            [weakSelf.payBombView remove];
            weakSelf.payBombView = nil;
            
            if (type == 1) {
                ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:str];
                vc.titleString = @"支付宝支付";
                [weakSelf by_normalPush:vc hasNavi:YES];
            }else{
                ByWechatPayQrView *vc = [[ByWechatPayQrView alloc] init];
                vc.title = @"微信支付";
                vc.str = str;
                [weakSelf by_normalPush:vc hasNavi:YES];
            }
            
        };
        
        _payBombView.selectBlock = ^(NSInteger index){
        
        };
    }
    return _payBombView;
}


- (void)initViews{
    
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
       
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-49);
        } else {
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(-49);
            
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    self.tableView.tableFooterView = footerView;
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [footerView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(footerView).offset(-25);
        make.centerY.equalTo(footerView).offset(0);
        make.size.mas_equalTo(CGSizeMake(50, 20));
    }];
    [button setTitle:@"规则说明" forState:UIControlStateNormal];
    [button setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = FONT(12);
    
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.tableView.mas_bottom).offset(0);
        make.width.mas_equalTo(@150);
        if (@available(ios 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
            make.bottom.equalTo(self.view).offset(0);
            
        }
    }];
    sureBtn.backgroundColor = rgba(237, 122, 19, 1);
    [sureBtn setTitle:@"发起跟单" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sureBtn.titleLabel.font = FONT(14);
    [sureBtn addTarget:self action:@selector(sureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
   
    UILabel *countLabel = [[UILabel alloc] init];
    [self.view addSubview:countLabel];
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.top.equalTo(sureBtn.mas_top).offset(0);
        make.right.equalTo(sureBtn.mas_left).offset(0);
        make.bottom.equalTo(sureBtn.mas_bottom).offset(0);
    }];
    countLabel.text = [NSString stringWithFormat:@"应付金额：%ld元",self.netModel.count * 2 * self.netModel.multiple.integerValue];
    countLabel.textColor = BY_THEME_COLOR;
    countLabel.textAlignment = NSTextAlignmentCenter;
    countLabel.font = FONT(14);
    countLabel.backgroundColor = [UIColor whiteColor];
    self.countLabel = countLabel;
    
    
}

- (void)sureBtnAction:(UIButton *)btn{
    

    if (self.textField.text.length > 0) {
        
        if (self.textField.text.integerValue < self.netModel.count * 2) {
            [ByCoreToolAlert showOnlyText:@"最低跟单金额最小不能小于单倍投注金额"];
            return;
        }
        
        
        self.netModel.low_amount = self.textField.text;
    }else{
        [ByCoreToolAlert showOnlyText:@"请输入最低跟单金额"];
        return;
    }
    
    if (self.oddsView.countLabel.text.length > 0) {
        self.netModel.ensure_odds = self.oddsView.countLabel.text;
    }else{
        [ByCoreToolAlert showOnlyText:@"请输入赔率"];
        return;
    }
    
    
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.numDic = self.netModel.numDic;
    netModel.multiple = self.netModel.multiple;
    netModel.free_pass = self.netModel.free_pass;
    netModel.low_amount = self.textField.text;
    netModel.ensure_odds = self.oddsView.countLabel.text;
    
    if (self.isBasketBall) {
        
         [self BasketballBetCheckWith:netModel];
        
    }else{
        
        [self footballBetCheckWith:netModel];
    }
   
}

- (void)setIsBasketBall:(BOOL)isBasketBall{
    _isBasketBall = isBasketBall;
}


- (void)footballBetCheckWith:(ByNetModel *)netModel1{
    [ByCoreToolAlert showLoadind:@"投注处理中" hasMasker:YES];
    [ByBallApi footballVerify:netModel1 success:^(ByNetBaseResult *result) {
        
        [ByCoreToolAlert hideHud];
        SLog(@"足球校验 = %@", result.data);
        NSDictionary *dictionary = result.data[@"data"];
        
        NSMutableArray *numArray = [NSMutableArray array];
        for (NSDictionary *dic in dictionary[@"list"]) {
            ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
            NSMutableDictionary *dataNum = [NSMutableDictionary dictionary];
            [dataNum setObject:[NSString stringWithFormat:@"%.2f",netModel.bonus.floatValue] forKey:@"bonus"];
            [dataNum setObject:netModel.content forKey:@"content"];
            [dataNum setObject:@"1" forKey:@"multiple"];
            [dataNum setObject:netModel.pass_type forKey:@"pass_type"];
            [dataNum setObject:netModel.strung forKey:@"strung"];
            [numArray addObject:dataNum];
        }
        
        [ByCoreToolAlert showLoadind:@"投注处理中" hasMasker:YES];
        netModel1.order_type = @"smart";
        netModel1.numArray = numArray;
        netModel1.reason = self.textView.text;
        netModel1.public_type = [NSString stringWithFormat:@"%ld", self.public_type];
        netModel1.origin_numDic = self.netModel.origin_numDic;
        [ByDocumentaryApi followOrderAddPlan:netModel1 success:^(ByNetBaseResult *result) {
            [ByCoreToolAlert hideHud];
            NSLog(@"跟单 = %@", result.data);
            
            self.orderModel = [[ByNetModel alloc] initContentWithDic:result.data[@"data"]];
            self.payBombView.moneyLabel.text = [NSString stringWithFormat:@"%@元", self.orderModel.amount];
            self.payBombView.orderModel = self.orderModel;
            [self.payBombView show];
            
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
        
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
}

- (void)BasketballBetCheckWith:(ByNetModel *)netModel1{
    [ByCoreToolAlert showLoadind:@"投注处理中" hasMasker:YES];
    [ByBallApi BasketBallVerify:netModel1 success:^(ByNetBaseResult *result) {
        
        [ByCoreToolAlert hideHud];
        SLog(@"足球校验 = %@", result.data);
        NSDictionary *dictionary = result.data[@"data"];
        
        NSMutableArray *numArray = [NSMutableArray array];
        for (NSDictionary *dic in dictionary[@"list"]) {
            ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
            NSMutableDictionary *dataNum = [NSMutableDictionary dictionary];
            [dataNum setObject:[NSString stringWithFormat:@"%.2f",netModel.bonus.floatValue] forKey:@"bonus"];
            [dataNum setObject:netModel.content forKey:@"content"];
            [dataNum setObject:@"1" forKey:@"multiple"];
            [dataNum setObject:netModel.pass_type forKey:@"pass_type"];
            [dataNum setObject:netModel.strung forKey:@"strung"];
            [numArray addObject:dataNum];
        }
        
        [ByCoreToolAlert showLoadind:@"投注处理中" hasMasker:YES];
        netModel1.order_type = @"smart";
        netModel1.numArray = numArray;
        netModel1.reason = self.textView.text;
        netModel1.public_type = [NSString stringWithFormat:@"%ld", self.public_type];
        netModel1.origin_numDic = self.netModel.origin_numDic;
        netModel1.lottery_type = @"2";
        [ByDocumentaryApi followOrderAddPlan:netModel1 success:^(ByNetBaseResult *result) {
            [ByCoreToolAlert hideHud];
            NSLog(@"跟单 = %@", result.data);
            
            self.orderModel = [[ByNetModel alloc] initContentWithDic:result.data[@"data"]];
            self.payBombView.moneyLabel.text = [NSString stringWithFormat:@"%@元", self.orderModel.amount];
            self.payBombView.orderModel = self.orderModel;
            [self.payBombView show];
            
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
        
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
}


- (void)btnAction:(UIButton *)btn{
    ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:[NSString stringWithFormat:@"%@/Wanfajiesao/gendan?type=0",BOYE_WEB_BASE_URL]];
    vc.titleString = @"跟单玩法";
    [self by_normalPush:vc hasNavi:YES];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.contentView.backgroundColor = BY_BACKGROUND_COLOR;
        
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UIView *backgroundView = [[UIView alloc] init];
    [cell.contentView addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(6);
        make.right.equalTo(cell.contentView).offset(-6);
        make.bottom.equalTo(cell.contentView).offset(0);
        make.top.equalTo(cell.contentView).offset(7);
    }];
    backgroundView.layer.cornerRadius = 10;
    backgroundView.backgroundColor = [UIColor whiteColor];
    
    ByDocumentaryCellTitleView *titleView = [[ByDocumentaryCellTitleView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 10, 75)];
    [backgroundView addSubview:titleView];
    titleView.nickLabel.text = BY_USER.nickname;
    [titleView.iconView sd_setImageWithURL:[NSURL URLWithString:BY_USER.head_pic] placeholderImage:[UIImage imageNamed:@"yonghutouxinag"]];
    titleView.timeLabel.hidden = YES;
    titleView.recordLabel.hidden = YES;
    
    
    ByDocumentaryContentView *contentView = [[ByDocumentaryContentView alloc] init];
    [backgroundView addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(0);
        make.right.equalTo(backgroundView).offset(0);
        make.top.equalTo(titleView.mas_bottom).offset(0);
        make.height.mas_equalTo(@150);
    }];
    
    
    if (self.isBasketBall) {
        contentView.titleLabel.text = @"竞彩篮球";
        if (self.type == 0) {
            contentView.titleValue1.valueLabel.text = @"混合投注";
        }else if (self.type == 1){
            contentView.titleValue1.valueLabel.text = @"胜负";
        }else if (self.type == 2){
            contentView.titleValue1.valueLabel.text = @"让胜负";
        }else if (self.type == 3){
            contentView.titleValue1.valueLabel.text = @"大小分";
        }else if (self.type == 4){
            contentView.titleValue1.valueLabel.text = @"胜负差";
        }
        
    }else{
        
        
         contentView.titleLabel.text = @"竞彩足球";
        if (self.type == 0) {// 0 混合投注 1 胜平负 2 让球胜平负  3 总进球 4 比分 5半全场
            contentView.titleValue1.valueLabel.text = @"混合投注";
        }else if (self.type == 1){
            contentView.titleValue1.valueLabel.text = @"胜平负";
        }else if (self.type == 2){
            contentView.titleValue1.valueLabel.text = @"让球胜平负";
        }else if (self.type == 3){
            contentView.titleValue1.valueLabel.text = @"总进球";
        }else if (self.type == 4){
            contentView.titleValue1.valueLabel.text = @"比分";
        }else if (self.type == 5){
            contentView.titleValue1.valueLabel.text = @"半全场";
        }
        
    }
    
    
   
    contentView.titleValue2.valueLabel.text = [NSString stringWithFormat:@"%ld元", self.netModel.count * 2];
    NSString *passString = [ByFootBallHandle getFootBallFreePassWithArray:self.netModel.selectedArray];
    contentView.titleValue3.valueLabel.text = [ByFootBallHandle getFootBallFreePassStringWithString:passString];
    
    UIView *grayView = [[UIView alloc] init];
    [backgroundView addSubview:grayView];
    [grayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(0);
        make.right.equalTo(backgroundView).offset(0);
        make.top.equalTo(contentView.mas_bottom).offset(0);
        make.bottom.equalTo(backgroundView).offset(0);
    }];
    grayView.backgroundColor = rgba(251, 251, 251, 1);
    
    ByDocumentaryOddsView *oddsView = [[ByDocumentaryOddsView alloc] init];
    [backgroundView addSubview:oddsView];
    [oddsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(0);
        make.right.equalTo(backgroundView).offset(0);
        make.top.equalTo(contentView.mas_bottom).offset(0);
        make.height.mas_equalTo(@45);
    }];
    self.oddsView = oddsView;
    
    CGFloat top = 75 + 150 + 45;
    for (int i = 0; i < 3; i++) {
       
        UILabel *titleLabel = [[UILabel alloc] init];
        [backgroundView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backgroundView).offset(15);
            make.top.equalTo(backgroundView).offset(top);
            make.height.mas_equalTo(@40);
            make.width.mas_equalTo(@90);
        }];
        
        top = top + 40;
        
        titleLabel.font = FONT(14);
        titleLabel.textColor = BY_TEXT_COLOR;
        if (i == 0) {
             titleLabel.text = @"最低跟单金额:";
        }else if (i == 1){
             titleLabel.text = @"保密设置:";
        }else if (i == 2){
             titleLabel.text = @"跟单理由:";
        }
       
    }
    
    
    UITextField *textField = [[UITextField alloc] init];
    [backgroundView addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(121);
        make.right.equalTo(backgroundView).offset(-20);
        make.top.equalTo(oddsView.mas_bottom).offset(4);
        make.height.mas_equalTo(@32);
    }];
    textField.layer.borderColor = BY_LINE_COLOR.CGColor;
    textField.layer.borderWidth = 1;
    textField.placeholder = @"请输入最低跟单金额";
    textField.keyboardType = UIKeyboardTypeNumberPad;
    self.textField = textField;
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 11, 32)];
    textField.leftView = leftView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.font = FONT(12);
    
    ByDocumentarySetChooseView *setView = [[ByDocumentarySetChooseView alloc] init];
    [backgroundView addSubview:setView];
    [setView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(textField.mas_left).offset(0);
        make.right.equalTo(textField.mas_right).offset(0);
        make.top.equalTo(textField.mas_bottom).offset(4);
        make.height.mas_equalTo(@40);
    }];
    setView.indexAtView = ^(NSInteger index){
        if (index == 0) {
            self.public_type = 2;
        }else if (index == 1){
            self.public_type = 1;
        }else if (index == 2){
            self.public_type = 0;
        }
    };
    
    
    UIView *textView = [[UIView alloc] init];
    [backgroundView addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(textField.mas_left).offset(0);
        make.right.equalTo(textField.mas_right).offset(0);
        make.top.equalTo(setView.mas_bottom).offset(10);
        make.bottom.equalTo(backgroundView).offset(-16);
    }];
    textView.layer.borderColor = BY_LINE_COLOR.CGColor;
    textView.layer.borderWidth = 1;
    textView.backgroundColor = [UIColor whiteColor];
    
    ByTextView *textView1 = [[ByTextView alloc] init];
    [textView  addSubview:textView1];
    textView1.textColor = [UIColor grayColor];
    textView1.font = FONT(12);
    textView1.placeholder = @"请输入跟单宣言（最多100字）";
    [textView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(textView).offset(10);
        make.right.equalTo(textView).offset(-10);
        make.top.equalTo(textView).offset(8);
        make.bottom.equalTo(textView).offset(-8);
    }];
    self.textView = textView1;
    
    
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    return 500;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
